//
//  main.swift
//  JDEachHelper
//
//  Created by Luoei on 2020/12/3.
//

import Foundation

let arr = ["A","B","C","D","E","F","G","H","I","J"]
let max = 11
let fruit = "MyFruit"
let pet = "MyPet"
let plantBean = "MyPlantBean"
let dreamFactory = "MyDreamFactory"
var target: [[String]] = []
var valueMap: [String:Int] = [:]

func run(){
    
    //MyFruits
    start(target: fruit, max: max, otherMax: 4, myMax: 5, targetKey: "ForOtherFruit")
    
    print("\n\n")
    
    start(target: pet, max: max, otherMax: 5, myMax: 5, targetKey: "ForOtherPet")
    
    print("\n\n")
    
    start(target: plantBean, max: max, otherMax: 3, myMax: 3, targetKey: "ForOtherPlantBean")
    
    print("\n\n")
    
    start(target: dreamFactory, max: max, otherMax: 3, myMax: 3, targetKey: "ForOtherDreamFactory")
    
    print("\n\n")
    print("Hello, World!")
}


func start(target: String, max: Int, otherMax: Int, myMax: Int, targetKey: String){
    var array: [String] = []
    for idx in 0..<max {
        if target == "MyDreamFactory", idx == 8 {
            continue
        }
        array.append("\(target)\(idx+1)")
    }
    // 初始化最大值
    array.forEach({ (value) in
        valueMap[value] = otherMax
    })
//    print("value map: \(valueMap)")
    
    var sortValueMap:[String: [String]] = [:]
    
    array.forEach { (value) in
//        print("value map: \(valueMap)")
        
        var item: [String] = []
        var currentValueMap:[String] = []
        
        for idx2 in 0..<otherMax {
            array.forEach { (keyx) in
                let count = valueMap[keyx]
                if value != keyx, !currentValueMap.contains(keyx), count == otherMax - idx2, count! > 0 {
                    currentValueMap.append(keyx)
                }
            }
            
        }
//        print("currentValueMap map: \(currentValueMap)")
        if currentValueMap.count > myMax {
            item = Array(currentValueMap[0..<myMax])
        }else{
            item = currentValueMap
        }
        
        item.forEach { (key) in
            var count = valueMap[key]!
            count -= 1
            if count < 0 {count = 0}
            valueMap[key] = count
            var arr = sortValueMap[key] ?? []
            arr.append(value)
            sortValueMap[key] = arr
        }
        
        print("\(value)需要助力的好友: \(item)")
        
    }
    
    print("value map: \(valueMap)")
    
    for (index, value) in array.enumerated() {
        var result = ""
        if let arr = sortValueMap[value] {
            arr.forEach { (keyx) in
                if result.count > 0 {
                    result += "\"\\\\@\"\\${\(keyx)}"
                }else{
                    result = "\\${\(keyx)}"
                }
            }
        }
        print("\(targetKey)\(index+1)=\(result)")
    }
    
    
}

run()



